package com.le.tester.controller;

import com.le.tester.common.ServerResponse;
import com.le.tester.sevice.RedissonSecondKillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 描述：使用redisson进行库存秒杀
 */
@Controller
@RequestMapping("/")
public class RedissonSecondKillController {

    private static final Logger LOG = LoggerFactory.getLogger(RedissonSecondKillController.class);

    @Autowired
    private RedissonSecondKillService redissonSecondKillService;

    @ResponseBody
    @RequestMapping("secondKill")
    public ServerResponse<Boolean> doSecondKill(@RequestParam("productId") Long productId,
                                                @RequestParam(value = "productQuantity", required = false) Integer productQuantity) {

        try {
            boolean isSecondKill = redissonSecondKillService.doSecondKill(productId, productQuantity);
            return ServerResponse.createBySuccess(isSecondKill);
        } catch (Exception e) {
            LOG.info("redisson second kill exception:{}", e.getMessage());
            return ServerResponse.createByErrorMessage(e.getMessage());
        }

    }

}
